package com.moleq.dialog;

import java.util.Calendar;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.mgpos.widget.NumberPadButton;
import com.moleq.mgpos.widget.NumberPadForPayment;
import com.moleq.mgpos.widget.NumberPadForPayment.NumberPadListener;
import com.moleq.mgpos.activities.R;
import com.moleq.util.MoleQUtil;

public class InputTimeInfoDialog extends MoleQBaseDialog {

	private OnResultListener mOnResultListener;
	private Button btnConfirm;
	private Button btnClose;
	private NumberPadForPayment mNumPad;
	private String age_time;
	private int viewid;
	private TextView tv_title;
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			switch (arg0.getId()) {
			case R.id.btn_time_close:
				dismiss();
				break;
			case R.id.btn_time_confirm:
				String strAuth = mNumPad.getBuffer();
				if (strAuth != null && !strAuth.equals("")) {
					age_time = changeTime(strAuth);
				}
				
				if(!age_time.equals(""))onCloseWithReturn(viewid,age_time);

				break;
			}
		}

	};
	
	private NumberPadListener mNumPadListener = new NumberPadListener(){

		@Override
		public void onClick(NumberPadButton button) {
			String time = mNumPad.getBuffer();
			switch(button){
			case ENTER:
//				String strAuth = mNumPad.getBuffer();
//				if(strAuth!=null && !strAuth.equals("")){
//					onCloseWithReturn(strAuth);
//				}
				break;
			case DOT:
				time = time.replace(".", "");
				mNumPad.setBuffer(time);
				break;
			}
			
		}
		
	};
	
	public String changeTime(String time){
		String age = "";
	    boolean bo = true;
	    if(time.length() != 4) bo = false;
	    time = "01011990"+time;
		age = MoleQUtil.fixDateTime2(time, "HH:mm", "MMddyyyyHHmm");
		 if(age.equals("")) bo = false;
		if(!bo){
			age= "";
			MessageBox.show(mContext, "The time is not vaild.");
		}
		return age;
	}
//	public void onCloseWithReturn(Object obj){
//		mOnResultListener.onReturnValue(DIALOG_INPUT_CHECK_INFO,obj);
//		dismiss();
//	}


	public InputTimeInfoDialog(Context context, Object base) {
		super(context,R.style.dialog,base);
		constructor(context,base);
	}
	
	public InputTimeInfoDialog(Context context, int theme,Object base) {
		super(context, theme,base);
		constructor(context,base);
	}
	
	private void constructor(Context context,Object base) {
		mContext = context;
		mOnResultListener = (OnResultListener) base;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_input_time_info);
		this.setCancelable(false);

		btnConfirm = (Button) findViewById(R.id.btn_time_confirm);
		btnConfirm.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.btn_time_close);
		btnClose.setOnClickListener(mOnClickListener);

		mNumPad = (NumberPadForPayment) findViewById(R.id.authNumPad);
		mNumPad.setNumberPadListener(mNumPadListener);
		tv_title = (TextView)findViewById(R.id.tv_title);
	}
	
	
	public void show(int vid,String timeText) {
		// TODO Auto-generated method stub
		super.show();
		
		Calendar calendar=Calendar.getInstance();
		int mHour=calendar.get(Calendar.HOUR_OF_DAY);
		int mMinute=calendar.get(Calendar.MINUTE);
		tv_title.setText("Time(HHdd)");
		
		viewid = vid;
		mNumPad.reset();
		mNumPad.requestFocusFromTouch();
		if (timeText.equals("")) {
			mNumPad.setBuffer(String.format("%02d", mHour)+String.format("%02d", mMinute));
		}else{
			mNumPad.setBuffer(timeText.replace(":", ""));
		}
		
	}
	
}
